<?php
/**
 * 用户接口相关
 */
namespace app\mobile\controller;

use dragon\Company\Entity\CompanyUser;
use dragon\Entity\Message;
use dragon\SysConfig;
use dragon\User\User;
use think\Db;

class UserController extends BaseController
{
    // 用户信息
    public function info() {
        $user = $this->user->toArray();
        unset($user['user_pass']);
//        $user['sex_format'] = dict_convert('sex', $user['sex']);
        $user['subscribe'] = $this->thirdPartyUser['subscribe'];
        return $this->success($user);
    }

    // 用户信息编辑
    public function save() {
        $post = $this->request->post();
        $this->user->save($post);

        return $this->info();
    }

    // message
    public function message() {
        $list = Message::where(['to_user_id' => $this->user->id])->order('is_new desc, create_time desc')->paginate($this->perpage);

//        if($message) {
//            $message['create_time'] = date('Y-m-d H:i:s', $message['create_time']);
//            Db::name('message')->where([
//                'id' => $message['id']
//            ])->update([
//                'is_read' => 1,
//                'is_new' => 0
//            ]);
//        }
        foreach($list as $key => $val) {
            $list[$key]['create_time_format'] = time_format('m-d', $val['create_time']);
        }

        return $this->success($list);
    }

    public function unReadMessage() {
        $total = Message::where(['to_user_id' => $this->user->id, 'is_read' => 0])->order('is_new desc, create_time desc')->count();
        return $this->success($total);
    }

    public function messageRead() {
        $id = $this->request->param('id', 0, 'intval');
        $message = Message::get([
            'to_user_id' => $this->user->id,
            'id' => ['in', $id]
        ]);
        $message->save([
            'is_read' => 1,
            'is_new' => 0
        ]);

        return $this->success('success');
    }

}
